package top.huiger.newday.activity

import android.support.v4.app.Fragment
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import top.huiger.newday.R
import top.huiger.newday.base.BaseActivity
import top.huiger.newday.fragment.AboutBaseFragment
import top.huiger.newday.fragment.GankBaseFragment
import top.huiger.newday.fragment.NewsBaseFragment
import top.huiger.newday.fragment.UtilsBaseFragment
import top.huiger.newday.widget.BottomMenu

class MainActivity : BaseActivity() {
    private var lastBMenu: BottomMenu? = null
    private lateinit var fragments: List<Fragment>

    override fun setViewId(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        fragments = arrayListOf(GankBaseFragment(), NewsBaseFragment(), UtilsBaseFragment(), AboutBaseFragment())

    }

    override fun initEvent() {
        bMenu_gank.setOnClickListener { v -> onClick(v) }
        bMenu_gank.performClick()
        bMenu_news.setOnClickListener { v -> onClick(v) }
        bMenu_utils.setOnClickListener { v -> onClick(v) }
        bMenu_about.setOnClickListener { v -> onClick(v) }

    }

    override fun loadData() {

    }

    private fun onClick(view: View) {
        val bMenu = view as BottomMenu
        bMenu.onSelect()

        if (lastBMenu !== view) {
            lastBMenu?.unSelect()
            lastBMenu = bMenu
        }

        val transaction = supportFragmentManager.beginTransaction()
        var tempFragment: Fragment ?= null

        when(view){
            bMenu_gank -> tempFragment = fragments[0]
            bMenu_news -> tempFragment = fragments[1]
            bMenu_utils -> tempFragment = fragments[2]
            bMenu_about -> tempFragment = fragments[3]
        }

        transaction.replace(R.id.content_layout, tempFragment)
        transaction.commit()
    }

}
